package com.spc.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.spc.constant.MessageConstant;
import com.spc.entity.PageResult;
import com.spc.entity.QueryPageBean;
import com.spc.entity.Result;
import com.spc.pojo.Role;
import com.spc.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//角色相关
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;
    //添加角色
    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        try {
            roleService.addRole(role,permissionIds,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
    }
    //查询所有角色
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
            PageResult page= roleService.findPage(queryPageBean);
            return page;
    }
    //根据查询所有角色
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role page= roleService.findById(id);
            return new Result(true,MessageConstant.FIND_ROLE_SUCCESS,page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.FIND_ROLE_FAIL);
        }
    }

    //根据查询关联表所有权限
    @RequestMapping("/findIds")
    public Result findIds(Integer id){
        try {
            List<Integer> list= roleService.findIds(id);
            return new Result(true,MessageConstant.FIND_ROLE_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.FIND_ROLE_FAIL);
        }
    }
    //修改角色
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        try {
            roleService.edit(role,permissionIds,menuIds);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    //删除角色
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    //根据角色id查询关联表所有菜单
    @RequestMapping("/findmenus")
    public Result findmenus(Integer id){
        try {
            List<Integer> list= roleService.findmenus(id);
            return new Result(true,MessageConstant.FIND_ROLE_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.FIND_ROLE_FAIL);
        }
    }
}
